package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.runner.Request;

import dataBase.Code;
import dataBase.User;
import dataBase.magaers.CodeDBMngr;

/**
 * Servlet implementation class UploadServlet
 */
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp?valid=unsigned");
		dispatcher.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		ServletOutputStream out = response.getOutputStream();
		if (session.getAttribute("email") == null){
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp?valid=unsigned");
			dispatcher.forward(request,response);
		}
		String title = request.getParameter("title");
		String language = request.getParameter("language");
		String isNewVersion = request.getParameter("isNewVersion");
		boolean visiblity = !((request.getParameter("visiblity"))== null);
		CodeDBMngr manager = CodeDBMngr.getInstance();
		int newVersion = 1;
		if (manager.containsTitle(title)){
			 int lastVersion = manager.getLastVersion(title);
			 if (lastVersion == -1){
				 out.println("<html><head><title>Problem in upload</title></head>" +
				 		"<body><h1>Sorry, there was a problem uploading your code, " +
				 		"please retry later :(</h1></body></html>");
			 }
			 newVersion = lastVersion +1;
		}
		ArrayList<String> categories = new ArrayList<String>(); 
		for (int i = 0; i < Code.CATEGORIES_NUMBER ; i++)
		{
			String temp = request.getParameter("category"+i);
			if (!temp.equals("")) {
				categories.add(temp);
			}
		}
		String code = request.getParameter("code");
		response.setContentType("text/html");
		out.println("<html><head><title>Upload</title></head>");
		
		manager.addNewCode(new Code(title, newVersion, language, code, (String)session.getAttribute("email"), visiblity, categories)); // new version = 1
		out.println("<body><h2>" + title + " Was Uploaded successfully<h2></body></html>");
	}
}